package com.codeape.number.base.mvvm.bindingadapter.ViewPager;

import androidx.databinding.BindingAdapter;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * ViewPager的ViewBindingAdapter
 *
 * @author LiuPeiming
 * Created at 2018/9/27 0:09
 */
public final class ViewBindingAdapter {

    /**
     * 设置adapter
     *
     * @param view    ViewPager
     * @param adapter FragmentPagerAdapter
     */
    @BindingAdapter({"adapter"})
    public static void setAdapter(ViewPager view, FragmentPagerAdapter adapter) {
        view.setAdapter(adapter);
    }

    /**
     * 设置选中item
     *
     * @param view ViewPager
     * @param item 选中item下标
     */
    @BindingAdapter("currentItem")
    public static void setCurrentItem(ViewPager view, int item) {
        view.setCurrentItem(item);
    }

    /**
     * 设置滑动监听
     *
     * @param view     ViewPager
     * @param listener ViewPager.OnPageChangeListener
     */
    @BindingAdapter("onPageChangeListener")
    public static void addOnPageChangeListener(ViewPager view, ViewPager.OnPageChangeListener listener) {
        view.addOnPageChangeListener(listener);
    }

    /**
     * 设置滑动动画
     *
     * @param view        ViewPager
     * @param transformer 滑动动画
     */
    @BindingAdapter("pageTransformer")
    public static void setPageTransformer(ViewPager view, ViewPager.PageTransformer transformer) {
        view.setPageTransformer(true, transformer);
    }

    /**
     * 设置预加载页面数量，防止切换页面时viewPager里的fragment重新加载
     *
     * @param view  ViewPager
     * @param limit 预加载页面数量
     */
    @BindingAdapter("offscreenPageLimit")
    public static void setOffscreenPageLimit(ViewPager view, int limit) {
        view.setOffscreenPageLimit(limit);
    }

}